/**
 * 行政老师获取年度列表
 * 
 * /xingzheng/year/getlist
 */

const Year = require('../../../models/years')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')

const get_years_list = async function (req, res) {
    try {
        var offset = 1
        var limit = parseInt(req.query.size)
        if (req.query.page > 1) {
            offset = parseInt(req.query.size) * (parseInt(req.query.page) - 1)
        } else if (req.query.page == 1) {
            offset = 0
        }

        const sql_res = await Year.findAndCountAll({
            where: {
                type: req.query.type
            },
            offset: offset,
            limit: limit,
            order: [
                ['createdAt', 'DESC'],
            ],
            attributes: ['id', 'createdAt', 'dead_line', 'type', 'name'],
        })

        res.json({
            code: 200,
            message: '列表获取成功',
            data: {
                list: sql_res.rows,
                total: sql_res.count
            }
        })

        // 日志
        utils.create_a_log(req, '年度管理', '浏览')
    }
    catch(error) {
        $.log('error :', error)
        res.json({
            code: 0000,
            message: '抓到异常',
            error: error
        })
        utils.create_a_log(req, '年度管理', '浏览', '失败')
    }
}
    
module.exports = get_years_list